﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SuperORM;
using System.Data;
using System.Text;
using System.Reflection;

namespace Helper
{
    public class CommonHelper
    {
        public static int GetMaxID(ISession m_session, string dsAlias, string dtname, string fieldName)
        {

            int maxid = 0;
            string sql = "select max(" + fieldName + ") from " + dtname;
            SmQuery m_query = m_session.CreateSmQuery(dsAlias);
            DataRowCollection drc = m_query.GetDataRowsBySQL(sql);
            if (drc != null && drc.Count > 0 && drc[0][0] != null && drc[0][0].ToString() != "")
            {
                maxid = Convert.ToInt32(drc[0][0].ToString());
            }
            maxid = maxid + 1;
            return maxid;
        }

        public static bool IsExistsRecord(ISession session, Type t, string keyvalue)
        {
            bool bexists = false;
            int id = -1;
            int.TryParse(keyvalue, out id);
            try
            {
                object o = session.Load(t, id);
                if (o != null) bexists = true;
            }
            catch
            {
            }
            return bexists;

        }

        public static bool IsExistsRecordEx(ISession session, string dsalias, string tablename, string keyfield, string keyvalue)
        {
            bool bexists = false;
            int id = -1;
            int.TryParse(keyvalue, out id);
            try
            {
                SmQuery qry = session.CreateSmQuery(dsalias);
                DataRowCollection drc = qry.GetDataRowsBySQL("select * from " + tablename + " where " + keyfield + "=" + keyvalue);
                if (drc != null && drc.Count > 0)
                    bexists = true;
            }
            catch
            {
            }
            return bexists;

        }

        public static string GetJsonStringFromTable(DataRowCollection drc)
        {
            /**/
            /**/
            /**/
            /* /****************************************************************************
          * Without goingin to the depth of the functioning of this Method, i will try to give an overview
          * As soon as this method gets a DataTable it starts to convert it into JSON String,
          * it takes each row and in each row it grabs the cell name and its data.
          * This kind of JSON is very usefull when developer have to have Column name of the .
          * Values Can be Access on clien in this way. OBJ.HEAD[0].<ColumnName>
          * NOTE: One negative point. by this method user will not be able to call any cell by its index.
         * *************************************************************************/
            StringBuilder JsonString = new StringBuilder();
            if (drc != null && drc.Count > 0)
            {
                DataTable dt = drc[0].Table;
                if (dt != null && dt.Rows.Count > 0)
                {
                    JsonString.Append("[ ");
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        JsonString.Append("{ ");
                        for (int j = 0; j < dt.Columns.Count; j++)
                        {
                            if (j < dt.Columns.Count - 1)
                            {
                                JsonString.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":" + "\"" + dt.Rows[i][j].ToString() + "\",");
                            }
                            else if (j == dt.Columns.Count - 1)
                            {
                                JsonString.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":" + "\"" + dt.Rows[i][j].ToString() + "\"");
                            }
                        }
                        /**/
                        /**/
                        /**/
                        /*end Of String*/
                        if (i == dt.Rows.Count - 1)
                        {
                            JsonString.Append("} ");
                        }
                        else
                        {
                            JsonString.Append("}, ");
                        }
                    }
                    JsonString.Append("]");

                }
                return JsonString.ToString();
            }
            else
            {
                return null;
            }
        }

        public static string GetJsonStringFromTable(DataTable dt)
        {
            /**/
            /**/
            /**/
            /* /****************************************************************************
          * Without goingin to the depth of the functioning of this Method, i will try to give an overview
          * As soon as this method gets a DataTable it starts to convert it into JSON String,
          * it takes each row and in each row it grabs the cell name and its data.
          * This kind of JSON is very usefull when developer have to have Column name of the .
          * Values Can be Access on clien in this way. OBJ.HEAD[0].<ColumnName>
          * NOTE: One negative point. by this method user will not be able to call any cell by its index.
         * *************************************************************************/
            StringBuilder JsonString = new StringBuilder();
            if (dt != null && dt.Rows.Count > 0)
            {
                JsonString.Append("[ ");
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    JsonString.Append("{ ");
                    for (int j = 0; j < dt.Columns.Count; j++)
                    {
                        string value = dt.Rows[i][j].ToString();
                        if (value.Contains("\""))
                        {
                            value = value.Replace("\"", "\\\\\\\"");
                        }
                        if (j < dt.Columns.Count - 1)
                        {
                            JsonString.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":" + "\"" + value + "\",");
                        }
                        else if (j == dt.Columns.Count - 1)
                        {
                            JsonString.Append("\"" + dt.Columns[j].ColumnName.ToString() + "\":" + "\"" + value + "\"");
                        }
                    }
                    /**/
                    /**/
                    /**/
                    /*end Of String*/
                    if (i == dt.Rows.Count - 1)
                    {
                        JsonString.Append("} ");
                    }
                    else
                    {
                        JsonString.Append("}, ");
                    }
                }
                JsonString.Append("]");

            }
            else
            {
                JsonString.Append("{[]}");
            }
            return JsonString.ToString();
        }

        public static string  GetFieldValueBySQL(string tablename,string selectfield, string condition)
        {
            string id = ZWL.DBUtility.DbHelperSQL.GetSHSL("select " + selectfield + " from "+tablename+" where "+condition);
            return id;
        }
       

    }
}